import { Home } from '../../types';
import * as types from '../action-types';
import { Action } from '../actions/Home';

let initState = {
    category: 'all',
    sliders: [],
    lessons: {
        hasMore: true,
        list: [],
        offset: 0,
        limit: 5,
        loading: false
    }
}

export default function (state:Home = initState, action:Action):Home {
    switch (action.type) {
        case types.SET_CATEGORY:
            return {...state, category: action.playload}
        case types.SET_SLIDERS:
            return {...state, sliders: action.playload}
        case types.SET_HOME_LESSONS_LOADING:
            return {...state, lessons:{...state.lessons, loading: true}}
        case types.SET_LESSONS:
            return {...state, lessons:{...state.lessons,
                hasMore: action.playload.hasMore,
                list: [...state.lessons.list, ...action.playload.list],
                offset: state.lessons.offset + action.playload.list.length,
                loading: false
            }}
        case types.RESET_LESSONS:
            console.log('getMore')
            return {...state, lessons:{...state.lessons,
                hasMore: action.playload.hasMore,
                list: action.playload.list,
                offset: action.playload.list.length,
                loading: false
            }}
        default:
            return state
    }
}